package com.sxj.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxj.ums.constant.ResponseConstant;
import com.sxj.ums.entity.User;
import com.sxj.ums.exception.PasswordErrorException;
import com.sxj.ums.exception.UserNotExistException;
import com.sxj.ums.mapper.UserMapper;
import com.sxj.ums.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 石小俊
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) throws UserNotExistException, PasswordErrorException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        List<User> users = userMapper.selectList(wrapper);
        if(users.isEmpty()){
            throw new UserNotExistException(ResponseConstant.RESPONSE_MESSAGE_USERNAME_ERROR);
        }
        User user = users.get(0);
        if(!password.equals(user.getPassword())){
            throw new PasswordErrorException(ResponseConstant.RESPONSE_MESSAGE_PASSWORD_ERROR);
        }
        return user;
    }
}
